﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace zielonka.co.uk.system.security.cryptography
{
    //Description:	Calculates the SHA1 hash value of a string and returns it as a base64 string.
    /*
        "Get the hash of this string".GetSHA1Hash();
    */

    public static partial class StringExtensions
    {
         /// <summary>
         /// Calculates the SHA1 hash of the supplied string and returns a base 64 string.
         /// </summary>
         /// <param name="stringToHash">String that must be hashed.</param>
         /// <returns>The hashed string or null if hashing failed.</returns>
         /// <exception cref="ArgumentException">Occurs when stringToHash or key is null or empty.</exception>
         public static string GetSHA1Hash(this string stringToHash)
         {
             if (String.IsNullOrEmpty(stringToHash))
             {
                 throw new ArgumentException("An empty string value cannot be hashed.");
             }

             Byte[] data = Encoding.UTF8.GetBytes(stringToHash);
             Byte[] hash = new SHA1CryptoServiceProvider().ComputeHash(data);
             return Convert.ToBase64String(hash);
         }
    }
}
 
